<template>

    <el-dialog
            v-model="isShowDialog"

            :title="title"
            width="30%"
            destroy-on-close
            center
    >
    <el-form label-width="80px" v-if="state==='msg'">
        <el-form-item label="您的ID:">
            {{userMsg.userId}}
        </el-form-item>
        <el-form-item label="您的账号:">
            {{userMsg.userAccount}}
        </el-form-item>
        <el-form-item label="您的姓名:">
            <el-input type="text" v-model="userMsg.userName" />
        </el-form-item>
        <el-form-item label="您的职位:">
            {{userMsg.roleId}}
        </el-form-item>
        <el-form-item label="所属部门:">
            {{userMsg.deptId}}
        </el-form-item>
    </el-form>
        <el-form label-width="80px" v-else-if="state==='password'">
            <el-form-item label="原密码:">
                <el-input type="text" v-model="updatePwd.oldPassword" show-password />
            </el-form-item>
            <el-form-item label="新密码:">
                <el-input type="text" v-model="updatePwd.newPassword" show-password />
            </el-form-item>
            <el-form-item label="重新输入:">
                <el-input type="text" v-model="updatePwd.cnewPassword" show-password />
            </el-form-item>
        </el-form>
      <span class="dialog-footer">
        <el-button type="primary" style="width: 100%" @click="updateUserName" v-if="state==='msg'"
        >确认修改</el-button
        >
          <el-button type="primary" style="width: 100%" @click="updateUserPwd" v-else-if="state==='password'"
          >确认修改</el-button
          >
      </span>
    </el-dialog>
</template>

<script>
    import utils from "@/Utils";
    export default {
        name: "UserMsg",
        props:{
            isShow:Boolean,
            title:String,
            state:String
        },
        data(){
            return {
                userMsg:{
                },
                updatePwd:{
                    oldPassword:'',
                    newPassword:'',
                    cnewPassword:''
                },
                utils
            }
        },
        computed:{
          isShowDialog: {
              get() {
                  return this.isShow
              },
              // setter
              set(newValue) {
                  this.$emit('close',newValue);
              }
          }
        },
        methods:{
            LoadLoginMsg:async function(){
                await this.axios.get("/User/isLogin/loginMsg").then((req)=>{
                    const data=req.data;
                    if (data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.userMsg=data.data;
                })
            },
            updateUserName:function () {
                this.axios.post("/User/isLogin/UpUserNameByUserId",{
                    UserName:this.userMsg.userName
                }).then((req)=>{
                    const data=req.data;
                    if (data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.utils.tipsMessage(data.data,data.code);
                    this.$router.go(0)
                })
            },
            updateUserPwd:function () {
                const updatePwd=this.updatePwd;
                if (this.utils.isEmpty(updatePwd.oldPassword, updatePwd.cnewPassword, updatePwd.newPassword)) {
                    this.utils.tipsMessage("请不要留空",1);
                    return;
                }
                if (this.utils.isRange(16, 6, updatePwd.newPassword, updatePwd.cnewPassword, updatePwd.oldPassword)) {
                    this.utils.tipsMessage("密码需要在6~16位之间",1);
                    return;
                }
                if (this.utils.isSpecialCharacters(updatePwd.newPassword, updatePwd.cnewPassword, updatePwd.oldPassword)) {
                    this.utils.tipsMessage("密码不能有特殊字符",1);
                    return;
                }
                if (this.utils.isTitleCase(updatePwd.newPassword, updatePwd.cnewPassword)) {
                    this.utils.tipsMessage("首字母需要大写",1);
                    return;
                }
                if (updatePwd.newPassword !== updatePwd.cnewPassword) {
                    this.utils.tipsMessage("请确认两次密码是否输入正确",1);
                    return;
                }
                this.axios.post("/User/isLogin/UpUserPasswordByUserId",{
                    oldPassword:updatePwd.oldPassword ,
                    newPassword:updatePwd.newPassword
                }).then((req)=>{
                    const data=req.data;
                    if (data.code>0){
                        this.utils.tipsMessage(data.error,data.code);
                        return
                    }
                    this.utils.tipsMessage(data.data,data.code);
                    this.$router.go(0)
                })
            }
        },
        mounted(){
            this.LoadLoginMsg();
        }
    }
</script>

<style scoped>

</style>